package cn.com.godliu.oauth2.config.jwt;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JdbcTokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;

import javax.sql.DataSource;

/**
 * 使用Jwt存储token的配置
 */
@Configuration
public class Jwt_TokenStore {

    @Autowired
    private DataSource dataSource;

    @Bean
    public TokenStore tokenStore() {

        //内存中保存token
        //return new JwtTokenStore(accessTokenConverter());

        //将token保存到数据库里
        return new JdbcTokenStore(dataSource);
    }

    @Bean
    public JwtAccessTokenConverter accessTokenConverter() {
        JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
        converter.setSigningKey("123");
        return converter;
    }

    /**
     * 自定义一些token信息
     */
    @Bean
    public Jwt_TokenEnhancer jwtTokenEnhancer() {
        return new Jwt_TokenEnhancer();
    }
}
